package cn.sunxyz.modules.handler;

import cn.sunxyz.modules.memory.DefaultNodeManager;
import cn.sunxyz.modules.memory.Node;
import cn.sunxyz.modules.memory.NodeManger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;

/**
 * Created by yangrd on 2017/5/11.
 */
public class SampleHandler {

    private static final Logger logger = LoggerFactory.getLogger(SampleHandler.class);

    private NodeManger<String> nodeManger = DefaultNodeManager.getInstance();

    public void bind(String value, String... friends) {
        nodeManger.bind(value, friends);
    }

    public String getMsg(String value) {
        Node<String> node = nodeManger.getNode(value);
        Set<Node<String>> nodes = node.getNodes();
        logger.debug("node#size : {} ", nodes.size());
        int max = 0;
        String msg = "";
        for (Node<String> node1 : nodes) {
            int count = nodeManger.usageCount(value, node1.getValue());
            logger.debug("msg: {} count: {}", node1.getValue(), count);
            if (max < count) {
                msg = node1.getValue();
                max = count;
                logger.debug("max :=> msg: {} count: {}", msg, max);
            }
        }
        return msg;

    }

}
